<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<dsl-navbar isBack="" title=""></dsl-navbar>
		<!-- #endif -->
		<!-- #ifdef H5 -->
		<dsl-navbar title=""></dsl-navbar>
		<!-- #endif -->
		<view class="login_title ">
			账号注册
		</view>
		<view class="login_content">
			欢迎使用源工云
		</view>
		<view style="margin-top: 200rpx;"></view>
		<view v-if="num ==0">
			<view class=" margin-lr-xl solid-bottom padding">
				<input type="text" value="" placeholder="请输入企业名称" v-model="username" />
			</view>
			<view class=" margin-lr-xl solid-bottom padding">
				<input type="number" value="" placeholder="请输入手机号" v-model="mobile" @confirm="submit" />
			</view>
			<view class=" margin-lr-xl solid-bottom padding difb alc align-center">
				<input type="text" value="" placeholder="验证码" v-model="captcha" style="width: 300rpx;"
					@confirm="submit" />
				<view class="code" @click="getCode()"> {{count||'获取验证码'}}</view>
			</view>
			<view class=" margin-lr-xl solid-bottom padding ">
				<input type="text" value="" placeholder="请输入密码" v-model="password" @confirm="submit" />
			</view>
		</view>
		<!-- 员工入驻 -->
		<view v-if="num ==1">
			<view class=" margin-lr-xl solid-bottom padding">
				<input type="number" value="" placeholder="请输入手机号" v-model="mobile" @confirm="submit" />
			</view>
			<view class=" margin-lr-xl solid-bottom padding difb alc align-center">
				<input type="text" value="" placeholder="验证码" v-model="captcha" style="width: 300rpx;"
					@confirm="submit" />
				<view class="code" @click="getCode()"> {{count||'获取验证码'}}</view>
			</view>
			<view class=" margin-lr-xl solid-bottom padding ">
				<input type="text" value="" placeholder="请输入密码" v-model="password" @confirm="submit" />
			</view>
		</view>




		<view class="submit_bg" @click="submit" hover-class="">
			注册
		</view>
		<!-- #ifdef H5 -->
		<view class="text-center" style="color: #00D88F;" @click="appUrl">
			App下载
		</view>
		<!-- #endif -->
		<!-- <view class="rowsb padding"> -->
		<!-- <view class="skip_register" @click="$tools.push('/pages/login/register')">立即注册</view> -->
		<!-- <view class="skip_password" @click="$tools.push('/pages/plugins/login/forget')">验证码登录</view> -->
		<!-- </view> -->



	</view>
</template>

<script>
	import {
		register,
		sms,
		getsystem
	} from '@/common/path.js'

	const app = getApp().globalData
	export default {
		data() {
			return {
				count: '',
				closeInput: false, //是否线索输入框删除
				isJurisdiction: true, //是否选中
				mobile: '', //手机号
				password: '', //密码
				captcha: '', //验证码
				username: '', //企业名称
				systemData: {}, //路径

				num: 0, //员工注册
			}
		},
		onLoad(option) {
			if (option.id) {
				this.num = option.num
				this.loginId = option.ids
			}
			this.system() //基础配置- 用于获取用户下载app链接
		},
		methods: {
			//基础配置
			system() {
				this.Http.POST(getsystem, {}).then(res => {
					console.log('[基础配置]', res);
					if (res.code == 1) {
						this.systemData = res.data
					}
				})
			},
			//下载
			appUrl() {
				let v = this
				uni.showActionSheet({
					itemList: ['源工云（人力端）下载', '源工云（求职端）下载', '源工云（企业端）下载'],
					success: function(res) {
						if (res.tapIndex == 0) {
							window.location.href = v.systemData.apply_url
						} else if (res.tapIndex) {
							window.location.href = v.systemData.manpower_url
						} else {
							window.location.href = v.systemData.firm_url
						}
					}
				});
			},

			getCode2() {
				const TIME_COUNT = 60;
				if (!this.timer) {
					this.count = TIME_COUNT;
					this.show = false;
					this.timer = setInterval(() => {
						if (this.count > 0 && this.count <= TIME_COUNT) {
							this.count -= 1;
						} else {
							this.show = true;
							clearInterval(this.timer);
							this.timer = null;
						}
					}, 1000);
				}
			},
			//获取验证码
			getCode() { //获取验证码
				this.Http.POST(sms, {
						mobile: this.mobile
					}, '加载中')
					.then(res => {
						if (res.code === 1) {
							this.$tools.showToast2('发送成功')
							this.getCode2()
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
			//注册
			submit() {

				// else if (this.username == '') {
				// 	this.$tools.showToast('请输入企业名称')
				// 	return
				// }
				if (this.mobile.length != 11) {
					this.$tools.showToast('请输入正确手机号')
				} else if (this.password == '') {
					this.$tools.showToast('请输入密码')
				} else if (this.captcha == '') {
					this.$tools.showToast('请输入验证码')
				} else {

					// #ifdef APP-PLUS
					let data = {
						mobile: this.mobile,
						password: this.password,
						captcha: this.captcha,
						typedata: '4', //身份:1=求职,2=用工企业,3=人力员工,4=人力公司,5=用工企业管理员,6=用工企业财务
						username: this.username,
					}
					this.Http.POST(register, data)
						.then(res => {
							console.log('[注册]', res);
							if (res.code == 1) {
								this.$tools.showToast2('注册成功')
								// #ifdef APP-PLUS

								setTimeout(r => {
									uni.navigateBack({
										delta: 1
									})
								}, 1000)
								// #endif
								// #ifdef H5
								this.appUrl()
								// #endif
							} else {
								this.$tools.showToast(res.msg)
							}
						})
					// #endif


					// #ifdef H5
					let v = this
					// uni.showActionSheet({
					// 	itemList: ['求职人申请', '用工企业','人力员工身份','人力公司','用工企业管理员','用工企业财务'],
					// 	success: function(res) {
					// 		v.typedata = res.tapIndex + 1
					// 	}
					// });
					let num = 0

					if (this.num == 0) num = 4 //人力公司
					if (this.num == 1) num = 3 //人力员工


					let data = {
						mobile: this.mobile,
						password: this.password,
						captcha: this.captcha,
						typedata: num, //身份:1=求职,2=用工企业,3=人力员工,4=人力公司,5=用工企业管理员,6=用工企业财务
						username: this.username,
						tuserid: this.loginId?this.loginId:'', //推荐人id
					}
					this.Http.POST(register, data)
						.then(res => {
							console.log('[注册]', res);
							if (res.code == 1) {
								this.$tools.showToast2('注册成功')
								// #ifdef APP-PLUS

								setTimeout(r => {
									uni.navigateBack({
										delta: 1
									})
								}, 1000)
								// #endif
								// #ifdef H5
								this.appUrl()
								// #endif
							} else {
								this.$tools.showToast(res.msg)
							}
						})
					// #endif


					// 1=求职,2=用工企业,3=人力员工,4=人力公司,5=用工企业管理员,6=用工企业财务
					// tuserid: this.loginId
				}


			},
			//微信登录
			submitWx() {
				let that = this
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						// console.log('登录', res);
						//支持微信、qq和微博等

						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
											that.third(loginRes.authResult
												.openid,
												infoRes.userInfo.avatarUrl,
												infoRes
												.userInfo.nickName)
										}
									});

								}

							})
						}
					},
					fail: (err) => {
						console.log('获取的登录方式', err)
					}
				});
			},
			// 微信授权登录
			third(wechat, url, name) {
				let data = {
					wechat: wechat,
					image: url,
					name: name,
				}
				this.$axios.axiosFrom('POST', third, data)
					.then(res => {
						if (res.code = 1) {
							this.$tools.showToast2('登录成功')
							console.log('获取到的用户信息====>', res.data);
							app.userInfo = res.data
							uni.setStorageSync('userInfo', res.data)
							uni.setStorageSync('token', res.data.token)
							setTimeout(r => {
								uni.navigateTo({
									url: '/pages/home/index'
								})
							}, 1000)
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
			inputMen(event) {
				console.log(event.detail.value);
				if (event.detail.value) this.closeInput = true
				else this.closeInput = false
			},
			JurisdictionMen() {
				this.isJurisdiction = !this.isJurisdiction
			}
		}
	}
</script>

<style>
	.code {
		height: 50rpx;
		width: 180rpx;
		background-color: #00D88F;
		border-radius: 50rpx;
	}

	.login_title {
		font-size: 70rpx;
		font-weight: bold;
		padding: 50rpx
	}

	.login_content {
		font-size: 30rpx;
		color: #3D444D;
		padding: 0rpx 50rpx;
	}

	.submit_bg {
		background-image: linear-gradient(to right, #00D88F, #00D88F);
		padding: 30rpx;
		color: white;
		text-align: center;
		margin: 30rpx;
		border-radius: 999rpx;
		box-shadow: 0px 1px 10px rgba(0, 216, 143, 0.5);
	}

	.skip_register {
		color: #00D88F;
	}

	.skip_password {
		color: #999999;
	}

	.skip_wx>image {
		width: 80rpx;
		height: 80rpx;
		margin: 0 30rpx;
	}

	.jurisdiction {
		color: #06121E;
		display: flex;
		align-items: center;
		margin-top: 100rpx;
	}

	.jurisdiction>image {
		width: 30rpx;
		height: 30rpx;
	}

	.jurisdiction>text {
		margin-left: 20rpx;
	}

	.code {
		width: 190rpx;
		height: 60rpx;
		background: #00D88F;
		opacity: 1;
		border-radius: 30rpx;
		color: white;
		line-height: 60rpx;
		text-align: center;
	}
</style>
